from jrcmodel.models import ArchivePerf, DvRatio
from decimal import Decimal

class ArchivePerformanceIterator:
    """
    Helps to iterate through performances, w/ calculations
    """
    _cursor = 0
    _performances = None
    _performances_total = 0
    
    _monthes_short = ["", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    PERFORMANCE_LEVERAGE_PCT = 5

    # Variables to accumulate values
    _unleverage_pct_sum = 100
    _max_unleverage_pct = 0
    _leverage_performance_pct_sum = 100
    _unleverage_drawdown = 0
    
    def __init__(self):
        self._performances = ArchivePerf.objects.all().order_by("year", "month")
#        print "Total performances: %d" % self._performances.count()
        
    def __iter__(self):
        return self
    
    def next(self):
        if self._cursor >= self._performances.count():
            raise StopIteration
        
        p =self._performances[self._cursor]
        self._cursor += 1
        
        current_unleverage_pct_sum = self._unleverage_pct_sum + p.value
        if self._max_unleverage_pct < current_unleverage_pct_sum:
            self._max_unleverage_pct = current_unleverage_pct_sum
        
        if current_unleverage_pct_sum < self._unleverage_pct_sum:
            self._unleverage_drawdown = self._max_unleverage_pct - current_unleverage_pct_sum
        
        leverage_performance_for_year = ""
        if p.month == 12:
            leverage_performance_for_year = p.calculate_leverage_performance_pct_year(p.year)

        unleverage_performance_for_year = ""
        if p.month == 12:
            unleverage_performance_for_year = p.calculate_unleverage_performance_pct_year(p.year)

        self._leverage_performance_pct_sum = self._leverage_performance_pct_sum + p.get_performance_leverage_pct()

        self._unleverage_pct_sum = current_unleverage_pct_sum
        
        return {
            "year": p.year
            , "month": p.month
            , "value": p.value
            , "month_short": self._monthes_short[p.month]
            , "unleverage_performance_for_year": unleverage_performance_for_year
            , "leverage_performance_for_year": leverage_performance_for_year
            , "unleverage_pct_sum": self._unleverage_pct_sum
            , "max_unleverage_pct": self._max_unleverage_pct
            , "leverage_performance_pct": p.value * self.PERFORMANCE_LEVERAGE_PCT
            , "leverage_performance_pct_sum": self._leverage_performance_pct_sum
            , "unleverage_drawdown": self._unleverage_drawdown
        }


class DvSoShRatioIterator:
    _cursor = 0
    _ratios = None
    
    _average_additional_performance = None
    _average_risk_free_interest_per_month = None
    
    def get_average_additional_performance(self):
        if self._average_additional_performance == None:
            total = 0
            for r in self._ratios:
                total = total + r.value
            print "Total add performance values: %f" % total
            self._average_additional_performance = total / self._ratios.count()
        return self._average_additional_performance
    
    def get_average_risk_free_interest_per_month(self):
        if self._average_risk_free_interest_per_month == None:
            total = 0
            for r in self._ratios:
                total = total + r.get_risk_free_interest_per_month()
            self._average_risk_free_interest_per_month = total / self._ratios.count()
        return self._average_risk_free_interest_per_month
    
    
    def get_current_var_additional_performance(self):
        """
            Returns current performance var to average
        """
        print "Cursor is on %d" % self._cursor
        if self._cursor >= self._ratios.count():
            return 0
        
        print "Additional perf: %f" % self._ratios[self._cursor].get_additional_performance()
        v = self.get_average_additional_performance() - self._ratios[self._cursor].get_additional_performance()
        v = v * v
        return v
    
    def __init__(self):
        self._cursor = 0
        self._ratios = DvRatio.objects.all().order_by("year", "month")
    
    def __iter__(self):
        return self
    

    def next(self):
        """
        Iterates through all DvRatio objects
        """
        if self._cursor >= self._ratios.count():
            raise StopIteration
        
        r = self._ratios[self._cursor]
        self._cursor += 1
        return r

